package com.hspedu.houserent.service;

import com.hspedu.houserent.domain.House;

/**
 * 数组保存信息
 */
public class HouseService {
    private House[] houses; //保存对象
    private int count;
    private final int size;

    public HouseService(int size) {
        houses = new House[size];
        count = 0;
        this.size = size;

        houses[count++] = new House(1,"jack","112","海淀区",2000,"未出租");
        houses[count++] = new House(2,"ajck","112","海淀区",1900,"未出租");
        houses[count++] = new House(3,"ckaj","112","海淀区",1800,"未出租");
        houses[count++] = new House(4,"kjac","112","海淀区",1700,"未出租");
        houses[count++] = new House(5,"kcaj","112","海淀区",1600,"未出租");


    }

    public House[] list() {
        return houses;
    }
    public void corr(int id,House house)
    {
        int id1 = houses[id].getId();
        houses[id] = house;
        houses[id].setId(id1);
    }

    public int rep(int id)
    {
        int index = -1;
        for (int i = 0; i < count; i++) {
            if (id == houses[i].getId()) {
                index = i;
            }
        }
        return index;
    }

    public boolean del(int delId) {
        //先找到房屋信息
        int index = rep(delId);
        if(index != -1){
            for (int i = index; i < count; i++) {
                houses[i] = houses[i + 1];
                System.out.println(houses[i]);
            }
            houses[count--] = null;
        }
        return true;
    }

    public boolean addHouse(String name, String phone, String address, int rend, String state) {
        if (count == size)
            return false;
        houses[count] = new House(count++, name, phone, address, rend, state);
        return true;
    }

    public House printHouse(int index)
    {
        return houses[index];
    }


}
